# 引入markdown模块
import markdown
from django.shortcuts import render

from article.models import ArticlePost


def article_detail(request, article_id):
    article = ArticlePost.objects.get(id=article_id)

    # 将markdown语法渲染成html样式
    article.body = markdown.markdown(article.body,
        extensions=[
        # 包含 缩写、表格等常用扩展
        'markdown.extensions.extra',
        # 语法高亮扩展
        'markdown.extensions.codehilite',
        ])

    context = { 'article': article }
    return render(request, 'article/detail.html', context)



# Create your views here.
def index(request):

    pictures = [
        "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/831b85275c1996abee8b0b3412500e24.jpg?thumb=1&w=1533&h=575&f=webp&q=90",
        "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/c66412360b4581510445386d6133ed4f.png?w=2452&h=920",
        "https://cdn.cnbj1.fds.api.mi-img.com/mi-mall/17e67a3d3c68bb81de533d71b0e73399.jpg?thumb=1&w=1533&h=575&f=webp&q=90"
    ]

    # 按创建时间的倒序获取分类为"专栏"的前10篇文章
    articles = ArticlePost.objects.filter(category__name="专栏").order_by('-created')[:10]

    # 按创建时间的倒序获取分类为"新闻资讯"的前10篇文章
    newspaper = ArticlePost.objects.filter(category__name="新闻资讯").order_by('-created')[:10]

    data = {
        'pictures': pictures,
        'articles': articles,
        'newspaper':newspaper,
    }

    return render(request, 'article/index.html', context=data)
